//
// Created by Scave on 2023/11/9.
//

#ifndef RIDER3D_RENDERCONTEXT_H
#define RIDER3D_RENDERCONTEXT_H

#include "Define.h"
#include "bgfx/bgfx.h"
#include "scene/Scene.h"
#include "shader/ShaderProgram.h"
#include "render/config/RenderConfig.h"

NS_RIDER_BEGIN

    struct BufferHandle {
        void* vertices = nullptr;
        void* indices = nullptr;
        // EntityConfig不使用动态顶点时才生效
        bgfx::VertexBufferHandle vertex_buffer_static = BGFX_INVALID_HANDLE;
        bgfx::IndexBufferHandle index_buffer_static = BGFX_INVALID_HANDLE;
    };

    class SceneRenderer;

    class RenderContext {
        friend SceneRenderer;
    public:
        explicit RenderContext(const RenderConfig& config);

        ~RenderContext();

        BufferHandle* CreateOrUpdateBuffer(Name& name,
                                           SMART_PTR<ShaderProgram>& shader_program,
                                           std::vector<Vertex>& vertex_list,
                                           std::vector<UInt16>& index_list);

        BufferHandle* GetBufferHandle(const Name& name);

        void PutBufferHandle(const Name& name, BufferHandle* handle);

        RenderConfig render_config_;
        Scene* scene_ = nullptr;
    private:
        HASHMAP<Name, BufferHandle*> name_buffer_handle_map_;

        static void RectifyVertices(std::vector<Vertex>& vertex_list);
    };

NS_RIDER_END

#endif //RIDER3D_RENDERCONTEXT_H
